import { Component,OnInit } from '@angular/core';
import { IndexService } from './index.service';
import { Pokemon } from '../pokemon/pockemon';
import { Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { Global } from 'src/global';

@Component({
  selector: 'app-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.css']
})
export class IndexComponent implements OnInit {
  constructor(private indexService: IndexService,private router: Router,private route: ActivatedRoute ) {   }

  h_p_page:number | undefined;
  p_page:number | undefined;
  h_l_page:number | undefined;
  l_page:number | undefined;

  toal_page:number|undefined;
  pages:Array<number> | undefined;
  pockemons: Pokemon[] = [];
  page:number=1;
  

  

  ngOnInit(): void {
    this.getPockemons(); //调用
  }
  addCollection(pockemon:Pokemon){
    const user=Global.user
    const collection={collection:{user:user,pockemon:pockemon}}
    // console.log(111)
    this.indexService.addCollection(collection).subscribe(data=>{})

  }
  disCollection(pockemon:Pokemon){
    const user=Global.user
    

  }

  getPockemons(page:number=1){
    this.router.navigate(['/index'], {queryParams: {page : page}});
  //   this.route.queryParams.subscribe(queryParams=> {
  //     console.log(queryParams.page);
  //  });
   
   this.page = page
   
  //  console.log("key", key);

    this.indexService.getPockemons(this.page).subscribe(
      data=>{
        if (data["stats"]=="ok"){
          this.pockemons=data['datas']
          this.h_p_page=data['h_p_page']
          this.p_page=data['p_page']
          this.h_l_page=data['h_l_page']
          this.l_page=data['l_page']
          this.toal_page=data['toal_page']
          this.pages=data['pages']
          this.page=data['page']
        }
        else{
          alert("获取数据失败")
        }
        
      }
    )
  }
}
